﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Common.Controls
{
    public partial class PostIt : UserControl
    {
        static readonly string default_title__ = "<Title>";
        static readonly string default_description__ = "";

        public PostIt(string title, string description)
        {
            InitializeComponent();

            this.BackColor = Color.Yellow;

            Title = title;
            txtDescription.Text = description;
        }

        public PostIt()
            : this(default_title__, string.Empty)
        {
        }

        private void ModifyTitle()
        {
            lblTitle.Visible = false;
            txtTitle.Cursor = Cursors.IBeam;
            txtTitle.ReadOnly = false;
            txtTitle.Focus();
            txtTitle.Select(int.MaxValue, 0);
        }

        private void ModifyDescription()
        {
            lblDescription.Visible = false;
            txtDescription.Cursor = Cursors.IBeam;
            txtDescription.ReadOnly = false;
            txtDescription.Focus();
            txtDescription.Select(int.MaxValue, 0);
        }

        private void txtTitle_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ModifyTitle();
        }

        private void lblDescription_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ModifyDescription();
        }

        private void txtTitle_Leave(object sender, EventArgs e)
        {
            txtTitle.ReadOnly = true;
            txtTitle.Cursor = Cursors.SizeAll;

            lblTitle.Visible = true;

            if (string.IsNullOrEmpty(Title.Trim()))
            {
                lblTitle.Text = default_title__;
            }
            else
            {
                lblTitle.Text = Title.Trim();
            }
        }

        private void txtDescription_Leave(object sender, EventArgs e)
        {
            txtDescription.ReadOnly = true;
            txtDescription.Cursor = Cursors.Arrow;

            lblDescription.Visible = true;

            if (string.IsNullOrEmpty(Description.Trim()))
            {
                lblDescription.Text = default_description__;
            }
            else
            {
                lblDescription.Text = Description.Trim();
            }
        }

        private void btnOptions_Click(object sender, EventArgs e)
        {
            ctxtMenuOptions.Show(btnOptions, new Point(0, btnOptions.Height));
        }

        [DefaultValue("")]
        public String Title
        {
            get { return txtTitle.Text; }
            set { txtTitle.Text = value; }
        }

        [DefaultValue("")]
        public String Description
        {
            get { return txtDescription.Text; }
            set { txtDescription.Text = value; }
        }

        [DefaultValue(typeof(Color), "Yellow")]
        public override Color BackColor
        {
            get
            {
                return base.BackColor;
            }
            set
            {
                txtTitle.BackColor = value;
                txtDescription.BackColor = Color.FromArgb(Math.Min(255, (int)(value.R + (255 - value.R) * 0.75)), Math.Min(255, value.G + (int)((255 - value.G) * 0.75)), Math.Min(255, value.B + (int)((255 - value.B) * 0.75)));
                lblDescription.BackColor = txtDescription.BackColor;
                btnOptions.BackColor = txtDescription.BackColor;
                base.BackColor = value;
            }
        }

        private bool mouse_grab_ = false;
        private Point mouse_location_;

        private void txtTitle_MouseDown(object sender, MouseEventArgs e)
        {
            if (txtTitle.ReadOnly && !mouse_grab_)
            {
                mouse_grab_ = true;
                mouse_location_ = e.Location;
            }
        }

        private void txtTitle_MouseUp(object sender, MouseEventArgs e)
        {
            mouse_grab_ = false;
        }

        private void txtTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouse_grab_)
            {
                // The location is relative to the Top Left corner of the control
                this.Location = new Point(this.Location.X + e.Location.X - mouse_location_.X, this.Location.Y + e.Location.Y - mouse_location_.Y);
            }
        }

        private void txtTitle_MouseLeave(object sender, EventArgs e)
        {
            mouse_grab_ = false;
        }
    }
}
